package com.neuedu.config;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;

import java.util.*;

public class NeueduRequest extends HttpServletRequestWrapper {
    private Map<String, String[]> paramsMap;

    public void addParameter(String key, String value) {
        if (paramsMap.containsKey(key)) {
            String[] values = paramsMap.get(key);
            int length = values.length;
            values = Arrays.copyOf(values, values.length + 1);
            values[length] = value;
        } else {
            paramsMap.put(key, new String[]{ value });
        }
    }
    public NeueduRequest(HttpServletRequest request) {
        super(request);
        paramsMap = new HashMap<>();
        Map<String, String[]> parameterMap = request.getParameterMap();
        Set<Map.Entry<String, String[]>> entries = parameterMap.entrySet();
        for (Map.Entry<String, String[]> entry : entries) {
            this.paramsMap.put(entry.getKey(), entry.getValue());
        }
    }

    @Override
    public String[] getParameterValues(String name) {
        if (paramsMap.containsKey(name)) {
            return paramsMap.get(name);
        }
        return null;
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        return paramsMap;
    }

    @Override
    public String getParameter(String name) {
        if (paramsMap.containsKey(name)) {
            return paramsMap.get(name)[0];
        }
        return null;
    }
}
